package com.terrier.ui.controls
{
    import com.terrier.ui.core.WxComponent;
    import com.terrier.ui.skins.IWxSkinBase;
    import com.terrier.ui.skins.IWxSlideButtonSkin;

    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.text.TextField;

    /**
     * 滑动按钮
     * @author Jarvis.weng
     */
    public class WxSlideButton extends WxComponent
    {
        private var _labelLeft:TextField;

        private var _labelRight:TextField;

        private var _dataLeft:*;

        private var _dataRight:*;

        private var _atRightSide:Boolean;

        public function WxSlideButton(leftLable:String, leftData:*, rightLabel:String, rightData:*)
        {
            _labelLeft = new TextField;
            _labelLeft.mouseEnabled = false;
            _labelLeft.text = leftLable;
            addChild(_labelLeft);

            _labelRight = new TextField;
            _labelRight.mouseEnabled = false;
            _labelRight.text = rightLabel;
            addChild(_labelRight);

            _dataLeft = leftData;
            _dataRight = rightData;

            addEventListener(MouseEvent.CLICK, clickButtonHandler);
        }

        override public function setSkin(value:IWxSkinBase):void
        {
            if (skin != null)
            {
                removeChildSafely(skin.bg);
                removeChildSafely(skin.slideBlock);
            }

            super.setSkin(value);

            if (skin != null)
            {
                _width = skin.bg.width;
                _height = skin.bg.height;

                var minIndex:int = Math.min(getChildIndex(_labelLeft), getChildIndex(_labelRight));
                addChildAt(skin.slideBlock, minIndex);
                addChildAt(skin.bg, minIndex);
            }

            invalidateView();
            invalidateLayout();
        }

        public function get currentData():*
        {
            return _atRightSide ? _dataRight : _dataLeft;
        }

        public function set currentData(value:*):void
        {
            var side:int = 0;
            if (value == _dataRight)
            {
                side = 1;
            }
            else if (value == _dataLeft)
            {
                side = -1;
            }

            if (side == 0 || _atRightSide == (side == 1))
            {
                return;
            }
            clickButtonHandler(null);
        }

        override protected function updateLayout():void
        {
            if (skin == null)
            {
                return;
            }

            var textWidth:int = (_width - skin.txtLeftPad - skin.txtRightPad) / 2;
            var textHeight:int = (_height - skin.txtTopPad - skin.txtBottomPad);

            _labelLeft.width = textWidth;
            _labelLeft.height = textHeight;
            _labelLeft.x = skin.txtLeftPad;
            _labelLeft.y = skin.txtTopPad;

            _labelRight.width = textWidth;
            _labelRight.height = textHeight;
            _labelRight.x = _width - skin.txtRightPad - _labelRight.width;
            _labelRight.y = skin.txtTopPad;

            skin.bg.width = _width;
            skin.bg.height = _height;

            skin.slideBlock.width = (_width - skin.slideLeftPad - skin.slideRightPad) / 2;
            skin.slideBlock.height = (_height - skin.slideTopPad - skin.slideBottomPad);
            skin.slideBlock.x = _atRightSide ? _width - skin.slideBlock.width - skin.slideRightPad : skin.slideLeftPad;
            skin.slideBlock.y = skin.slideTopPad;
        }

        override protected function updateView():void
        {
            if (_atRightSide)
            {
                skin.onTxtFmt.applyStyle(_labelRight);
                skin.offTxtFmt.applyStyle(_labelLeft);
            }
            else
            {
                skin.onTxtFmt.applyStyle(_labelLeft);
                skin.offTxtFmt.applyStyle(_labelRight);
            }
        }

        private function clickButtonHandler(e:MouseEvent):void
        {
            startAnimtion();
        }

        private function animation(e:Event):void
        {
            if (skin == null)
            {
                return;
            }

            // 更新滑块坐标
            var speed:int = _atRightSide ? -30 : 30;
            skin.slideBlock.x += speed;

            // 检测滑块位置
            var limit:int;
            var complete:Boolean;
            if (_atRightSide)
            {
                limit = skin.slideLeftPad;
                if (skin.slideBlock.x <= limit)
                {
                    complete = true;
                }
            }
            else
            {
                limit = _width - skin.slideBlock.width - skin.slideRightPad;
                if (skin.slideBlock.x >= limit)
                {
                    complete = true;
                }
            }

            if (complete)
            {
                skin.slideBlock.x = limit;
                stopAnimation();
            }
        }

        private function startAnimtion():void
        {
            addEventListener(Event.ENTER_FRAME, animation);
        }

        private function stopAnimation():void
        {
            removeEventListener(Event.ENTER_FRAME, animation);

            _atRightSide = !_atRightSide;
            invalidateView();
        }

        private function get skin():IWxSlideButtonSkin
        {
            return skin as IWxSlideButtonSkin;
        }
    }
}
